#include "Udpclient.hpp"

int main(int argc, char *argv[])
{
    std::string server_ip = "127.0.0.1";
    uint16_t server_port = 8080;

    if (argc >= 2)
    {
        server_ip = argv[1];
    }
    if (argc >= 3)
    {
        server_port = static_cast<uint16_t>(std::stoi(argv[2]));
    }

    UdpClient client;

    std::cout << "正在连接" << server_ip << ":" << server_port << std::endl;
    if (!client.connect(server_ip, server_port))
    {
        std::cerr << "连接服务器失败" << std::endl;
        return 1;
    }
    std::cout << "连接成功" << std::endl;

    std::string input;
    std::string response;

    std::cout << "输入消息发送到服务器(输入exit退出):" << std::endl;

    while (true)
    {
        std::cout << "> ";
        std::getline(std::cin, input);

        if (input == "exit")
        {
            break;
        }

        if (!client.send(input))
        {
            std::cerr << "发送消息失败" << std::endl;
            continue;
        }

        std::cout << "消息已发送" << std::endl;

        if (client.recv(response))
        {
            std::cout << "服务器响应:" << response << std::endl;
        }
        else
        {
            std::cerr << "接收响应失败" << std::endl;
        }
    }

    std::cout << "断开连接" << std::endl;
    client.disconnect();
    std::cout << "已断开连接" << std::endl;

    return 0;
}